﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Zeus.Biz.DAL.EF.Northwind;
using Zeus.Biz.Northwind.Model;
using Zeus.Biz.Northwind.Model.ViewModel;
using Zeus.Biz.Northwind.BLL;
using System.Threading;

namespace Zeus.Mvc.Web.Controllers
{
    public class EmployeeController : Controller
    {
        NorthwindContext context = new NorthwindContext("NorthwindConnection");

        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(string txtLastName, string txtFirstName)
        {
            Employee employee = new Employee
            {
                LastName = txtLastName,
                FirstName = txtFirstName
            };

            context.Employees.Add(employee);
            context.SaveChanges();

            EmployeePage pageModel = new EmployeePage(1, 10);
            EmployeeViewModel employeeInfo = pageModel.GetRecordSet();
             
            return RedirectToAction("ListAll", new { pageIndex = employeeInfo.TotalPageNum, pageSize = employeeInfo.PageSize }); 
        }

        public ActionResult ListAll(string pageIndex, string pageSize)
        {
            EmployeePage pageModel = new EmployeePage(Convert.ToInt32(pageIndex), Convert.ToInt32(pageSize));
            EmployeeViewModel employeeInfo = pageModel.GetRecordSet();
            
            ViewBag.CurrentIndex = Convert.ToInt32(pageIndex) - 1;

            return View(employeeInfo);
        }

        public ActionResult ShowPage(string pageIndex, string pageSize)
        {
            EmployeePage pageModel = new EmployeePage(Convert.ToInt32(pageIndex), Convert.ToInt32(pageSize));
            EmployeeViewModel employeeInfo = pageModel.GetRecordSet();

            return View("/Views/Controls/ctlEmpInfo.ascx", employeeInfo);
        }

        public ActionResult ListAllAjax(string pageIndex, string pageSize)
        {
            EmployeePage pageModel = new EmployeePage(Convert.ToInt32(pageIndex), Convert.ToInt32(pageSize));
            EmployeeViewModel employeeInfo = pageModel.GetRecordSet();

            ViewBag.CurrentIndex = Convert.ToInt32(pageIndex) - 1;

            return View(employeeInfo);
        }

        public ActionResult ShowPageAjax(string pageIndex, string pageSize)
        {
            EmployeePage pageModel = new EmployeePage(Convert.ToInt32(pageIndex), Convert.ToInt32(pageSize));
            EmployeeViewModel employeeInfo = pageModel.GetRecordSet();

            Thread.Sleep(3000);

            return View("/Views/Controls/ctlEmpInfo.ascx", employeeInfo);
        }
    }
}
